package com.i2863.tools.weixinmp.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExUtil {

	private static Pattern getPattern(String regEx) {
		return  Pattern.compile(regEx, Pattern.MULTILINE
				| Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
		
	}

	public static boolean find(String content, String regEx) {
		Pattern p = getPattern(regEx);
		Matcher m = p.matcher(content);
		return m.find();
	}

	public static boolean matches(String content, String regEx) {
		Pattern p = getPattern(regEx);
		Matcher m = p.matcher(content);
		return m.matches();
	}

	public static String getFirstValue(String content, String regEx, int index) {
		Pattern p = getPattern(regEx);
		Matcher m = p.matcher(content);
		String value = "";
		if (m.find()) {
			value = m.group(index);
			if (value != null) {
				return value.trim();
			}
		}
		return value;
	}

	public static String getLastValue(String content, String regEx, int index) {
		Pattern p = getPattern(regEx);
		Matcher m = p.matcher(content);
		String value = null;
		while (m.find()) {
			value = m.group(index).trim();
		}
		return value;
	}

	public static String[] getFirstArray(String content, String regEx) {
		Pattern p = getPattern(regEx);
		Matcher m = p.matcher(content);
		String[] results = null;
		if (m.find()) {
			List<String> groupList = new ArrayList<String>();
			for (int i = 0; i <= m.groupCount(); i++) {
				groupList.add(m.group(i));
			}
			results = groupList.toArray(new String[groupList.size()]);
		}
		return results;
	}

	public static String[] getLastArray(String content, String regEx) {
		Pattern p = getPattern(regEx);
		Matcher m = p.matcher(content);
		String[] results = null;
		while (m.find()) {
			List<String> groupList = new ArrayList<String>();
			for (int i = 0; i <= m.groupCount(); i++) {
				groupList.add(m.group(i));
			}
			results = groupList.toArray(new String[groupList.size()]);
		}
		return results;
	}

	public static String[] getIndexArray(String content, String regEx, int index) {
		Pattern p = getPattern(regEx);
		List<String> values = new ArrayList<String>();
		Matcher m = p.matcher(content);
		while (m.find()) {
			values.add(m.group(index).trim());
		}
		return values.toArray(new String[values.size()]);
	}

	public static String[][] getAllValues(String content, String regEx) {
		Pattern p = getPattern(regEx);
		Matcher m = p.matcher(content);
		List<List<String>> groupList = new ArrayList<List<String>>();
		String[][] results = null;
		while (m.find()) {
			List<String> currList = new ArrayList<String>();
			for (int i = 0; i <= m.groupCount(); i++) {
				currList.add(m.group(i));
			}
			groupList.add(currList);
		}
		results = new String[groupList.size()][];
		for (int i = 0; i < results.length; i++) {
			results[i] = new String[groupList.get(i).size()];
			groupList.get(i).toArray(results[i]);
		}
		return results;
	}
	public static void main(String[] args) {
		System.out.println(getFirstValue("剩余3天以上", "([0-9]+)天", 1));
	}
}